﻿namespace USElections.Command
{
    using System;
    using System.Linq;
    using System.Windows.Input;
    using Microsoft.Phone.Shell;
    using USElections.Model;
    
    /// <summary>
    /// PinToStartCommand class.
    /// </summary>
    public class PinToStartCommand : ICommand
    {
        #region Constructor
        /// <summary>
        /// Initializes a new instance of the PinToStartCommand class.
        /// </summary>
        public PinToStartCommand()
        {
        }
        #endregion

        #region Event
        /// <summary>
        /// CanExecuteChanged event.
        /// </summary>
        public event EventHandler CanExecuteChanged;
        #endregion

        #region Public Methods
        /// <summary>
        /// Check if command can be executed.
        /// </summary>
        /// <param name="parameter">The TrialWrapper item.</param>
        /// <returns>True if command can be executed.</returns>
        public bool CanExecute(object parameter)
        {
            Candidate candidate = parameter as Candidate;

            if (candidate != null)
            {
                if (!string.IsNullOrEmpty(candidate.ImageUri))
                {
                    ShellTile currentTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(Model.Enums.Variables.CandidateId.ToString() + "=" + candidate.Id));

                    if (currentTile == null)
                    {
                        return true;
                    }
                }
            }

            return false;
        }
        
        /// <summary>
        /// Execute command.
        /// </summary>
        /// <param name="parameter">The TrialWrapper item.</param>
        public void Execute(object parameter)
        {
            Candidate candidate = parameter as Candidate;

            if (candidate != null && !string.IsNullOrEmpty(candidate.ImageUri))
            {
                // Create the tile object and set some initial properties for the tile.
                // The Count value of 12 will show the number 12 on the front of the Tile. Valid values are 1-99.
                // A Count value of 0 will indicate that the Count should not be displayed.
                StandardTileData newTileData = new StandardTileData
                {
                    Title = candidate.FullName,
                    BackTitle = "US Elections",
                    BackBackgroundImage = new Uri("/Background.png", UriKind.Relative),
                    BackgroundImage = new Uri(candidate.ImageUri, UriKind.Relative)
                };

                // Create the tile and pin it to Start. This will cause a navigation to Start and a deactivation of our application.
                ShellTile.Create(new Uri("/Pages/MainPage.xaml?" + Model.Enums.Variables.CandidateId.ToString() + "=" + candidate.Id, UriKind.Relative), newTileData);
            }
        }

        /// <summary>
        /// Revalidate if command can be executed.
        /// </summary>
        public void RevalidateCanExecute()
        {
            if (this.CanExecuteChanged != null)
            {
                this.CanExecuteChanged(this, null);
            }
        }
        #endregion
    }
}
